#include<stdio.h>
#include <string.h>
#include"menu.h"
#include"telephoneBook.h"

bool IsNameEqual(Tinof* inof,void* value)
{
    char *name = (char *)value;
    if (strcmp(inof->Name,name)==0)
        return true;
    else
        return false;
}
bool IsPhoneEqual(Tinof* inof,void* value)
{
    char *phone = (char *)value;
    if (strcmp(inof->Telephone,phone)==0)
        return true;
    else
        return false;
}
bool IsNameHave(Tinof* inof,void* value)
{
    char *name = (char *)value;
    if (strstr(inof->Name,name))
        return true;
    else
        return false;
}
bool IsPhoneHave(Tinof* inof,void* value)
{
    char *phone = (char *)value;
    if (strstr(inof->Telephone,phone))
        return true;
    else
        return false;
}
int main()
{
    // FILE *fp;
    // fp = fopen("Contacts.txt", "w"); 
    // if (fp == NULL) {
    //     printf("文件打开失败");
    //     return 1;
    // }
    TBook book;
    InitTBook(&book);
    InsertInof(&book, "张三", "123456");
    InsertInof(&book, "李四", "132434");
    InsertInof(&book, "王五", "132451");
    InsertInof(&book, "张四", "123457");
    int expression;
    char name[NameSize] = {0};
    char phone[TelephoneSize] = {0};
    char newname[NameSize] = {0};
    char newphone[TelephoneSize] = {0};
    do
    {
    menu_main();
    printf("请输入您需要的服务序号：");
    scanf("%d", &expression);
    getchar();
    switch (expression)
    {
    case 1:
        Show(&book);
        break;
    case 2:
        printf("请输入姓名：");
        scanf("%s", name);
        FindPeople(&book, name, IsNameHave);
        break;
    case 3:
        printf("请输入电话号码：");
        scanf("%s", phone);
        FindPeople(&book, phone, IsPhoneHave);
        break;
    case 4:
        printf("请输入姓名：");
        scanf("%s", name);
        printf("请输入电话号码：");
        scanf("%s", phone);
        InsertInof(&book, name,phone);
        break;
    case 5:
        printf("请输入姓名：");
        scanf("%s", name);
        RemovePeople(&book, name, IsNameEqual);
        break;
    case 6:
        printf("请输入电话号码：");
        scanf("%s", phone);
        RemovePeople(&book, phone, IsPhoneEqual);
        break;
    case 7:
        printf("请输入旧的姓名：");
        scanf("%s", name);
        printf("请输入新的姓名：");
        scanf("%s", newname);
        RevisePeople(&book, "姓名",&name,&newname,IsNameEqual);
        break;
    case 8:
        printf("请输入旧的电话号码：");
        scanf("%s", phone);
        printf("请输入新的电话号码：");
        scanf("%s", newphone);
        RevisePeople(&book, "电话号码",&phone,&newphone,IsPhoneEqual);
        break;
    case 9:
        printf("感谢您的使用！\n");
        FreeBook(&book);
        return 0;
    default:
        printf("您输入的序号不对，请重新输入！\n");
        break;
    }
    }while (expression != 9);
    return 0;
}